﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NTwitter;
using System.Collections.ObjectModel;
using System.Drawing;
using TwitterClient2.Controller;

namespace TwitterClient2.Components
{
    public class PanelUserOverview : Panel
    {
        private VScrollBar scrollBarUserOverview;
        private List<PanelUserOverviewUser> userChilds = new List<PanelUserOverviewUser>();
        private int userChildsLength = 0;
        private byte location;
        private TwitterClientController controller = TwitterClientController.getInstance();
        public PanelUserOverview(byte location)
        {
            this.location = location;
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.scrollBarUserOverview = new System.Windows.Forms.VScrollBar();
            this.SuspendLayout();
            // 
            // scrollBarUserOverview
            // 
            this.scrollBarUserOverview.Location = new System.Drawing.Point(210, 0);
            this.scrollBarUserOverview.Name = "scrollBarUserOverview";
            this.scrollBarUserOverview.Size = new System.Drawing.Size(10, 240);
            this.scrollBarUserOverview.TabIndex = 0;
            this.scrollBarUserOverview.ValueChanged += new System.EventHandler(this.scrollBarUserOverview_ValueChanged);
            // 
            // PanelUserOverview
            // 
            this.BackColor = System.Drawing.Color.Gainsboro;
            this.Controls.Add(this.scrollBarUserOverview);
            this.Size = new System.Drawing.Size(220, 240);
            this.ResumeLayout(false);

        }

        public void uploadData(Collection<User> users)
        {
            if (this.userChilds.Count > 0)
            {
                foreach (PanelUserOverviewUser userPanel in this.userChilds)
                {
                    this.Controls.Remove(userPanel);
                }
            }
            this.userChilds = new List<PanelUserOverviewUser>();
            for (int i = 0; i < users.Count; i++)
            {
                PanelUserOverviewUser userPanel = new PanelUserOverviewUser(users[i]);
                userPanel.Click += new EventHandler(this.userClicked);
                userPanel.PictureBoxUser.Click += new EventHandler(this.userClicked);
                if (i % 2 == 0)
                {
                    userPanel.Location = new Point(23, 10 + this.userChildsLength);
                    if (i == (users.Count - 1))
                    {
                        this.userChildsLength += 80;
                    }
                }
                else
                {
                    userPanel.Location = new Point(116, 10 + this.userChildsLength);
                    this.userChildsLength += 80;
                }
                this.userChilds.Add(userPanel);
                this.Controls.Add(userPanel);
            }
            this.Refresh();
        }



        private void userClicked(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            PanelUserOverviewUser userPanel;
            if (sender is PictureBox) { userPanel = (PanelUserOverviewUser)(((PictureBox)sender).Parent); } else { userPanel = (PanelUserOverviewUser)sender; }

            TwitterClientEvent tEvent = new TwitterClientEvent();
            tEvent.paramList.Add(this.location);
            tEvent.paramList.Add(userPanel.User);
            this.controller.performUserInput(TwitterClientController.USER_CLICK_FRIENDS_FOLLOWERS, tEvent);
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }

        private void scrollBarUserOverview_ValueChanged(object sender, EventArgs e)
        {
            if (this.userChildsLength > 80)
            {
                int userChildsLengthTemp = -(this.userChildsLength * this.scrollBarUserOverview.Value) / 100;
                for (int i = 0; i < userChilds.Count; i++)
                {


                    if (i % 2 == 0)
                    {
                        userChilds[i].Location = new Point(23, 10 + userChildsLengthTemp);
                    }
                    else
                    {
                        userChilds[i].Location = new Point(116, 10 + userChildsLengthTemp);
                        userChildsLengthTemp += 80;
                    }

                }
                this.Refresh();
            }
        }
    }
}
